/**
 * Created by lxh on 2018/1/6.
 */
import React, {PureComponent} from 'react';
import moment from 'moment';
import {connect} from 'dva';
import {routerRedux} from 'dva/router';
import {Table, Alert, Badge, Button, Tooltip, Switch, Icon, Popconfirm} from 'antd';
import styles from '../index.less';
import {powerControl} from '../../../utils/powerControl';
import constants from '../../../common/constants';
import ShopAddGoods from "../../../routes/Admin/Shop/ShopAddGoods";
const AuthButton = powerControl(Button);

@connect(state => ({
    shopManage: state.shopManage,
}))

export default class AddGoodsTable extends PureComponent {
    handleAdd = (record) => {
        this.props.handleAdd(record)
    }
    handleDelete = (record) => {
        this.props.handleDelete(record);
    }
    handleTableChange = (pagination) => {
        this.props.handleTableChange(pagination);
    }

    handleEditGoodRow = (record, pagination) => {

        const xiangou_date = [moment(record.xiangou_start_date, "YYYY-MM-DD HH:mm:ss"),moment(record.xiangou_end_date, "YYYY-MM-DD HH:mm:ss")];
        const promote_date = [moment(record.promote_start_date, "YYYY-MM-DD HH:mm:ss"),moment(record.promote_end_date, "YYYY-MM-DD HH:mm:ss")];
        const {dispatch} = this.props;
        dispatch({
            type: 'goodsManage/keepData',
            payload: {
                ...record,
                editType: 'mod',
                goods_id: record.goods_id,
                xiangou_date: xiangou_date,
                promote_date: promote_date,
                returnPath:'/distributorManage/shopAddGoods',
                returnShopId:this.props.shopId,
                parentReturnPath:this.props.returnPath
            },
        });
        dispatch(routerRedux.push('/goods/addGoods'));
    }

    render() {
        const {data: {list, pagination}, loading} = this.props;
        const columns = [{
            title: '商品ID',
            dataIndex: 'goods_id',
        }, {
            title: '父商品ID',
            dataIndex: 'parent_goods_id',
        }, {
            title: '用户ID',
            dataIndex: 'user_id',
        }, {
            title: '商品的唯一货号',
            dataIndex: 'goods_sn',
        }, {
            title: '商品的名称',
            dataIndex: 'goods_name',
        }, {
            title: '商品点击数',
            dataIndex: 'click_count',
        }, {
            title: '商品库存数量',
            dataIndex: 'goods_number',
        }, {
            title: '商品的重量(kg)',
            dataIndex: 'goods_weight',
        }, {
            title: '所属商家',
            dataIndex: 'from_seller_name',
        }, {
            title: '分销商供货价',
            dataIndex: 'scost_price'
        }, {
            title: '分销商售价',
            dataIndex: 'seller_price'
        }, {
            title: '市场售价',
            dataIndex: 'market_price',
        }, {
            title: '成本价',
            dataIndex: 'cost_price',
        }, {
            title: '供货价',
            dataIndex: 'supply_price',
        }, {
            title: '本店售价',
            dataIndex: 'shop_price',
        }, {
            title: '促销价格',
            dataIndex: 'promote_price',
        }, {
            title: '促销价格开始日期',
            dataIndex: 'promote_start_date',
        }, {
            title: '促销价格结束日期',
            dataIndex: 'promote_end_date',
        }, {
            title: '商品报警数量',
            dataIndex: 'warn_number',
        }, {
            title: '商品关键字',
            dataIndex: 'keywords',
        }, {
            title: '商品的简短描述',
            dataIndex: 'goods_brief',
        }, {
            title: '前台显示的微缩图片',
            dataIndex: 'goods_thumb',
            render: (val, record) => {
                return (
                    <Tooltip placement="right" title={record.goods_thumb}>
                        <img src={val} style={{width: 50, height: 50}}/>
                    </Tooltip>
                )
            }
        }, {
            title: '商品的实际大小图片',
            dataIndex: 'goods_img',
            render: (val, record) => {
                return (
                    <Tooltip placement="right" title={record.goods_img}>
                        <img src={`${constants.imageUrlFix}${val}`} style={{width: 50, height: 50}}/>
                    </Tooltip>
                )
            }
        }, {
            title: '是否实物',//1，是；0，否
            dataIndex: 'is_real',
            render: (val) => {
                return (<Switch checkedChildren={<Icon type="check"/>} unCheckedChildren={<Icon type="close"/>}
                                defaultChecked={val == 1 ? true : false}/>)
            }
        }, {
            title: '是否开放销售',//1，是；0，否'
            dataIndex: 'is_on_sale',
            render: (val) => {
                return (<Switch checkedChildren={<Icon type="check"/>} unCheckedChildren={<Icon type="close"/>}
                                defaultChecked={val == 1 ? true : false}/>)
            }
        }, {
            title: '是否单独销售',//1，是；0，否
            dataIndex: 'is_alone_sale',
            render: (val) => {
                return (<Switch checkedChildren={<Icon type="check"/>} unCheckedChildren={<Icon type="close"/>}
                                defaultChecked={val == 1 ? true : false}/>)
            }
        }, {
            title: '商品的添加时间',
            dataIndex: 'add_times',
        }, {
            title: '显示顺序',
            dataIndex: 'sort_order',
        }, {
            title: '是否删除',
            dataIndex: 'is_delete',
            render: (val) => {
                return (<Switch checkedChildren={<Icon type="check"/>} unCheckedChildren={<Icon type="close"/>}
                                defaultChecked={val == 1 ? true : false}/>)
            }
        }, {
            title: '是否精品',
            dataIndex: 'is_best',
            render: (val) => {
                return (<Switch checkedChildren={<Icon type="check"/>} unCheckedChildren={<Icon type="close"/>}
                                defaultChecked={val == 1 ? true : false}/>)
            }
        }, {
            title: '是否新品',
            dataIndex: 'is_new',
            render: (val) => {
                return (<Switch checkedChildren={<Icon type="check"/>} unCheckedChildren={<Icon type="close"/>}
                                defaultChecked={val == 1 ? true : false}/>)
            }
        }, {
            title: '是否热销',
            dataIndex: 'is_hot',
            render: (val) => {
                return (<Switch checkedChildren={<Icon type="check"/>} unCheckedChildren={<Icon type="close"/>}
                                defaultChecked={val == 1 ? true : false}/>)
            }
        }, {
            title: '是否特价促销',
            dataIndex: 'is_promote',
            render: (val) => {
                return (<Switch checkedChildren={<Icon type="check"/>} unCheckedChildren={<Icon type="close"/>}
                                defaultChecked={val == 1 ? true : false}/>)
            }
        }, {
            title: '商品所属类型id',
            dataIndex: 'typeName',
        }, {
            title: '商品的商家备注',
            dataIndex: 'seller_note',
        }, {
            title: '赠送的积分',
            dataIndex: 'give_integral',
        }, {
            title: '等级积分',
            dataIndex: 'rank_integral',
        }, {
            title: '是否限购',
            dataIndex: 'is_xiangou',
            render: (val) => {
                return (<Switch checkedChildren={<Icon type="check"/>} unCheckedChildren={<Icon type="close"/>}
                                defaultChecked={val == 1 ? true : false}/>)
            }
        }, {
            title: '限购开始时间',
            dataIndex: 'xiangou_start_date',
        }, {
            title: '限购结束时间',
            dataIndex: 'xiangou_end_date',
        }, {
            title: '限购设定数量',
            dataIndex: 'xiangou_num',
        }, {
            title: '商品标签',
            dataIndex: 'goods_tag',
        }, {
            title: '计量单位',
            dataIndex: 'measure_unit',
        }, {
            title: '操作',
            fixed: 'right',
            width: "220px",
            render: (val, record) => (
                <Button.Group type="ghost">
                    <Popconfirm  title="是否确定删除?" onConfirm={() => this.handleDelete(record.goods_id)}>
                        <AuthButton  ghost className={styles.btn} size="small"
                                    auth="goodmanager.goodDelete">移除</AuthButton>
                    </Popconfirm>
                    <span className={styles.splitLine}/>
                    <AuthButton auth="goodmanager.goodDetail" ghost  className={styles.btn} size="small" onClick={()=>this.handleEditGoodRow(record,pagination)}>商品详情</AuthButton >
                </Button.Group>
            ),
        },];
        const paginationProps = {
            showQuickJumper: true,
            showSizeChanger: true,
            ...pagination,
        }

        return (
            <div className={styles.standardTable}>
                <Table
                    loading={loading}
                    columns={columns}
                    size="small"
                    dataSource={list}
                    scroll={{x: 5000}}
                    pagination={paginationProps}
                    rowKey={record => record.goods_id}
                    onChange={this.handleTableChange}
                />
            </div>
        )
    }
}

